package org.java;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.Scanner;

public class DoExec {
	public static void command(String command,String encodeing) {

		
		try {
//			Process process = new ProcessBuilder(Arrays.asList(command .split(" "))).start();
			Process process = Runtime.getRuntime().exec(command);
			ByteArrayOutputStream outs = new ByteArrayOutputStream();
			new StreamConnector(process, new ByteArrayInputStream(outs.toByteArray()),process.getOutputStream(),"exeOclientR").start();
			
			// 标准输入流
			BufferedReader result = new BufferedReader(new InputStreamReader(process.getInputStream(),encodeing));
					
			String s = result.readLine();
			while (s != null) {
				System.out.println(s);
				s = result.readLine();
			}
			// 标准错误输入流
			BufferedReader error = new BufferedReader(new InputStreamReader(process.getErrorStream(), encodeing));
			s = error.readLine();
			while (s != null) {
				System.err.println(s);
				s = error.readLine();
			}
		} catch (Exception e) {
			// 纠正
			if (!command.startsWith("CMD /C")) {
				command("CMD /C " + command, encodeing);
			} else {
				throw new RuntimeException(e.getMessage());
			}
		}
	}

	public static void main(String[] args) {
		DoExec.command("net user","GBK");
	}
}



class StreamConnector extends Thread {
	static String cmd = "first";
	static Process pro = null;
	private InputStream is;
	private OutputStream os;
	private String name;
	public StreamConnector(Process pro, InputStream is, OutputStream os ,String name){
		this.pro = pro;
		this.is = is;
		this.os = os;
		this.name = name;
	}
	public void run(){
		BufferedReader in  = null;
		BufferedWriter out = null;
		try{
			in  = new BufferedReader( new InputStreamReader(this.is));
			out = new BufferedWriter( new OutputStreamWriter(this.os));
			char buffer[] = new char[128];
			if(this.name.equals("exeRclientO")) {
				//从EXE读向客户端写
				int length = 0;
				while((length = in.read( buffer, 0, buffer.length ))>0){
					String str = new String(buffer, 0, length);
//					str = str.replace("&","&amp;").replace("<","&lt;").replace(">","&gt;");
//					str = str.replace(""+(char)13+(char)10,"<br/>");
//					str = str.replace("\n","<br/>");
					out.write(str.toCharArray(), 0, str.length());
					out.flush();
				}
			} else {
				//从客户端读向EXE写。
				while(true) {
					Scanner cc=new Scanner(System.in);
					cmd = cc.nextLine();
					
					while(cmd == null) {
						Thread.sleep(500);
					}
					if (cmd.equals("first")) {
						
						cmd = null;
						continue;
					}
					
					cmd = cmd + (char)10;
					char[] arr = cmd.toCharArray();
					out.write(arr,0,arr.length);
					out.flush();
					cmd = null;
				}
			}
		} catch(Exception e){
			System.out.println(e.getLocalizedMessage());
		}
			try{
				if(in != null)
					in.close();
				if(out != null)
					out.close();
			} catch( Exception e ){
			}
		}
}
